1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.apache.tapestry5.internal;
16
17 import org.apache.tapestry5.*;
18 import org.apache.tapestry5.beaneditor.Width;
19 import org.apache.tapestry5.internal.test.InternalBaseTestCase;
20 import org.apache.tapestry5.ioc.Messages;
21 import org.apache.tapestry5.ioc.Resource;
22 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
23 import org.apache.tapestry5.ioc.services.TypeCoercer;
24 import org.apache.tapestry5.runtime.ComponentResourcesAware;
25 import org.testng.annotations.DataProvider;
26 import org.testng.annotations.Test;
27
28 import java.util.*;
29 import java.util.regex.Pattern;
30
31 public class TapestryInternalUtilsTest extends InternalBaseTestCase
32 {
33 @Test(dataProvider = "to_user_presentable_data")
34 public void to_user_presentable(String input, String expected)
35 {
36 assertEquals(TapestryInternalUtils.toUserPresentable(input), expected);
37 }
38
39 @DataProvider
40 public Object[][] to_user_presentable_data()
41 {
42 return new Object[][]
43 {
44 {"hello", "Hello"},
45 {"userId", "User Id"},
46 {"useHTML", "Use HTML"},
47 {"underscored_name", "Underscored Name"},};
48 }
49
50 @Test
51 public void map_from_keys_and_values()
52 {
53 Map<String, String> map = TapestryInternalUtils.mapFromKeysAndValues("fred", "flintstone", "barney", "rubble");
54
55 assertEquals(map.size(), 2);
56 assertEquals(map.get("fred"), "flintstone");
57 assertEquals(map.get("barney"), "rubble");
58 }
59
60 @Test
61 public void string_to_option_model_just_label()
62 {
63 OptionModel model = TapestryInternalUtils.toOptionModel("Just A Label");
64
65 assertEquals(model.getLabel(), "Just A Label");
66 assertEquals(model.getValue(), "Just A Label");
67 }
68
69 @Test
70 public void string_to_option_model()
71 {
72 OptionModel model = TapestryInternalUtils.toOptionModel("my-value=Some Label");
73
74 assertEquals(model.getLabel(), "Some Label");
75 assertEquals(model.getValue(), "my-value");
76 }
77
78 @Test
79 public void string_to_option_models()
80 {
81 List<OptionModel> options = TapestryInternalUtils.toOptionModels("UK,USA,DE=Germany");
82
83 assertEquals(options.size(), 3);
84
85 assertEquals(options.get(0).getLabel(), "UK");
86 assertEquals(options.get(0).getValue(), "UK");
87
88 assertEquals(options.get(1).getLabel(), "USA");
89 assertEquals(options.get(1).getValue(), "USA");
90
91 assertEquals(options.get(2).getLabel(), "Germany");
92 assertEquals(options.get(2).getValue(), "DE");
93 }
94
95 @Test
96 public void map_entry_to_option_model()
97 {
98 Map<String, String> map = Collections.singletonMap("key", "value");
99 Map.Entry entry = map.entrySet().iterator().next();
100 OptionModel model = TapestryInternalUtils.toOptionModel(entry);
101
102 assertEquals(model.getLabel(), "value");
103 assertEquals(model.getValue(), "key");
104 }
105
106 @Test
107 public void map_to_option_models()
108 {
109 Map<Integer, String> map = new TreeMap<Integer, String>();
110 map.put(1, "A");
111 map.put(2, null);
112 map.put(3, "C");
113
114 List<OptionModel> options = TapestryInternalUtils.toOptionModels(map);
115
116 assertEquals(options.size(), 3);
117
118 assertEquals(options.get(0).getLabel(), "A");
119 assertEquals(options.get(0).getValue(), 1);
120
121 assertEquals(options.get(1).getLabel(), "");
122 assertEquals(options.get(1).getValue(), 2);
123
124 assertEquals(options.get(2).getLabel(), "C");
125 assertEquals(options.get(2).getValue(), 3);
126 }
127
128 @Test
129 public void null_map_key_is_null_option_value()
130 {
131
132 Map<Integer, String> map = new HashMap<Integer, String>();
133 map.put(null, "Label");
134
135 List<OptionModel> options = TapestryInternalUtils.toOptionModels(map);
136
137 assertEquals(options.size(), 1);
138
139 assertEquals(options.get(0).getLabel(), "Label");
140 assertEquals(options.get(0).getValue(), null);
141 }
142
143 @Test
144 public void object_to_option_model()
145 {
146 Object object = new Integer(27);
147 OptionModel model = TapestryInternalUtils.toOptionModel(object);
148
149 assertEquals(model.getLabel(), "27");
150 assertEquals(model.getValue(), object);
151 }
152
153 @Test
154 public void list_to_option_models()
155 {
156 List<String> list = new ArrayList<String>();
157 list.add("A");
158 list.add(null);
159 list.add("C");
160
161 List<OptionModel> options = TapestryInternalUtils.toOptionModels(list);
162
163 assertEquals(options.size(), 3);
164
165 assertEquals(options.get(0).getLabel(), "A");
166 assertEquals(options.get(0).getValue(), "A");
167
168 assertEquals(options.get(1).getLabel(), "");
169 assertEquals(options.get(1).getValue(), null);
170
171 assertEquals(options.get(2).getLabel(), "C");
172 assertEquals(options.get(2).getValue(), "C");
173 }
174
175 @Test
176 public void whitespace_around_terms_is_trimmed()
177 {
178 List<OptionModel> options = TapestryInternalUtils.toOptionModels(" UK , USA , DE=Germany ");
179
180 assertEquals(options.size(), 3);
181
182 assertEquals(options.get(0).getLabel(), "UK");
183 assertEquals(options.get(0).getValue(), "UK");
184
185 assertEquals(options.get(1).getLabel(), "USA");
186 assertEquals(options.get(1).getValue(), "USA");
187
188 assertEquals(options.get(2).getLabel(), "Germany");
189 assertEquals(options.get(2).getValue(), "DE");
190 }
191
192 @Test
193 public void string_to_select_model_type_coercion_integration()
194 {
195 TypeCoercer coercer = getService(TypeCoercer.class);
196
197 SelectModel selectModel = coercer.coerce(" UK , USA , DE=Germany ", SelectModel.class);
198
199 assertNull(selectModel.getOptionGroups());
200 assertEquals(selectModel.getOptions().size(), 3);
201
202
203 }
204
205 @Test
206 public void parse_key_value()
207 {
208 KeyValue kv = TapestryInternalUtils.parseKeyValue("foo=bar");
209
210 assertEquals(kv.getKey(), "foo");
211 assertEquals(kv.getValue(), "bar");
212 }
213
214 @Test
215 public void bad_format_key_value_pair()
216 {
217 String input = "abraxas";
218
219 try
220 {
221 TapestryInternalUtils.parseKeyValue(input);
222 unreachable();
223 } catch (IllegalArgumentException ex)
224 {
225 assertEquals(ex.getMessage(), "Key/value pair 'abraxas' is not properly formatted (it does not contain an equals sign).");
226 }
227 }
228
229 @Test
230 public void whitespace_trimmed_for_key_value()
231 {
232 KeyValue kv = TapestryInternalUtils.parseKeyValue(" mykey = myvalue ");
233
234 assertEquals(kv.getKey(), "mykey");
235 assertEquals(kv.getValue(), "myvalue");
236 }
237
238 @Test
239 public void extract_id_from_property_expression()
240 {
241 assertEquals(TapestryInternalUtils.extractIdFromPropertyExpression("simpleName"), "simpleName");
242 assertEquals(TapestryInternalUtils.extractIdFromPropertyExpression("complex.name().withStuff"),
243 "complexnamewithStuff");
244 assertEquals(TapestryInternalUtils.extractIdFromPropertyExpression("number99.withABullet"),
245 "number99withABullet");
246 }
247
248 @Test
249 public void default_label_key_found()
250 {
251 Messages messages = mockMessages();
252 train_contains(messages, "myid-label", true);
253 train_get(messages, "myid-label", "My Id");
254
255 replay();
256
257 assertEquals(TapestryInternalUtils.defaultLabel("myid", messages, "myid-name-not-used"), "My Id");
258
259 verify();
260 }
261
262 @Test
263 public void default_label_from_name()
264 {
265 Messages messages = mockMessages();
266
267 stub_contains(messages, false);
268
269 replay();
270
271 assertEquals(TapestryInternalUtils.defaultLabel("foobarbazbiff", messages, "foo.bar().baz.biff()"), "Biff");
272
273 verify();
274 }
275
276 @Test
277 public void null_equals_null()
278 {
279 assertTrue(TapestryInternalUtils.isEqual(null, null));
280 }
281
282 @Test
283 public void non_null_never_equals_null()
284 {
285 assertFalse(TapestryInternalUtils.isEqual(this, null));
286 }
287
288 @Test
289 public void same_is_equal()
290 {
291 assertTrue(TapestryInternalUtils.isEqual(this, this));
292 }
293
294 @Test
295 public void is_equal_with_objects()
296 {
297 String left = "left";
298 String right = "right";
299
300 assertFalse(TapestryInternalUtils.isEqual(left, right));
301 assertTrue(TapestryInternalUtils.isEqual(left, new String(left)));
302 }
303
304 @Test
305 public void type_coersion_string_to_pattern()
306 {
307 TypeCoercer coercer = getObject(TypeCoercer.class, null);
308
309 String input = "\\s+";
310
311 Pattern pattern = coercer.coerce(input, Pattern.class);
312
313 assertEquals(pattern.toString(), input);
314 }
315
316 @Test
317 public void type_coersion_from_component_resources_aware_to_component_resources()
318 {
319 ComponentResourcesAware input = newMock(ComponentResourcesAware.class);
320 ComponentResources resources = mockComponentResources();
321
322 expect(input.getComponentResources()).andReturn(resources);
323
324 TypeCoercer coercer = getObject(TypeCoercer.class, null);
325
326 replay();
327
328 ComponentResources actual = coercer.coerce(input, ComponentResources.class);
329
330 assertSame(actual, resources);
331
332 verify();
333 }
334
335 @Test
336 public void to_class_attribute_value_empty()
337 {
338 List<String> classes = Collections.emptyList();
339
340 assertNull(TapestryInternalUtils.toClassAttributeValue(classes));
341 }
342
343 @Test
344 public void to_class_attribute_value_normal()
345 {
346 List<String> classes = CollectionFactory.newList("fred", "barney", "wilma");
347
348 assertEquals(TapestryInternalUtils.toClassAttributeValue(classes), "fred barney wilma");
349 }
350
351 @DataProvider
352 public Object[][] split_at_commas_data()
353 {
354 return new Object[][]
355 {
356 {"foo", new String[]
357 {"foo"}},
358 {"foo, bar", new String[]
359 {"foo", "bar"}},
360 {" foo, \nbar\t\t", new String[]
361 {"foo", "bar"}},
362 {"", new String[0]},
363 {null, new String[0]}};
364 }
365
366 @Test(dataProvider = "split_at_commas_data")
367 public void split_at_commas(String input, String[] output)
368 {
369 assertArraysEqual(TapestryInternalUtils.splitAtCommas(input), output);
370
371 }
372
373 @DataProvider
374 public Object[][] to_base64_data()
375 {
376 return new Object[][]
377 {
378 {0L, "AA"},
379 {1L, "AQ"},
380 {0xab54a98ceb1f0ad2L, "q1SpjOsfCtI"}};
381 }
382
383 @Test
384 public void to_asset2_no_wrapper_needed()
385 {
386 Asset2 asset2 = mockAsset2();
387
388 replay();
389
390 assertSame(TapestryInternalUtils.toAsset2(asset2), asset2);
391
392 verify();
393 }
394
395 @Test
396 public void asset_to_asset2_wrapper()
397 {
398 Asset asset = mockAsset();
399 Resource resource = mockResource();
400 String clientURL = "clientURL";
401
402 train_toClientURL(asset, clientURL);
403
404 expect(asset.getResource()).andReturn(resource);
405
406 replay();
407
408 Asset2 asset2 = TapestryInternalUtils.toAsset2(asset);
409
410 assertFalse(asset2.isInvariant());
411
412 assertSame(asset2.toClientURL(), clientURL);
413 assertSame(asset2.toString(), asset.toString());
414 assertSame(asset2.getResource(), resource);
415
416 verify();
417 }
418
419 @Test
420 public void to_internal_property_conduit_no_wrapper_needed()
421 {
422 InternalPropertyConduit conduit2 = newMock(InternalPropertyConduit.class);
423
424 assertSame(TapestryInternalUtils.toInternalPropertyConduit(conduit2), conduit2);
425 }
426
427 @Test
428 public void to_internal_property_conduit_wrapper()
429 {
430 PropertyConduit conduit = mockPropertyConduit();
431
432 Integer result = 123;
433 Width width = newMock(Width.class);
434
435 expect(conduit.get("")).andReturn(result);
436 expect(conduit.getAnnotation(Width.class)).andReturn(width);
437 expect(conduit.getPropertyType()).andReturn(Integer.class);
438 conduit.set("", 345);
439
440 replay();
441
442 InternalPropertyConduit conduit2 = TapestryInternalUtils.toInternalPropertyConduit(conduit);
443
444 assertNull(conduit2.getPropertyName());
445
446 assertSame(conduit2.get(""), result);
447 assertSame(conduit2.getAnnotation(Width.class), width);
448 assertSame(conduit2.getPropertyType(), Integer.class);
449 conduit2.set("", 345);
450
451 verify();
452 }
453
454 @Test
455 public void toFileSuffix_tests()
456 {
457 assertEquals(TapestryInternalUtils.toFileSuffix("foo.tar.gz"), "gz");
458 assertEquals(TapestryInternalUtils.toFileSuffix("just-a-file"), "");
459 }
460 }